package com.linran.gclearn.jvm.oom;

/**
 * -Xss128k
 * 栈大小改为128k
 * 不管栈深度测试还是栈容量测试都是产生StackOverflowError错误
 */
public class JavaVMStackSOF {
    private int stackLength = 1;

    public void stackDepthLeak() {
        //栈深度测试:StackOverflowError
        stackLength++;
        stackDepthLeak();
    }

    public void stackCapacity() {
        //多定义变量使每个栈帧容量变大
        //栈容量测试:StackOverflowError
        int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98,a99,a100;
        stackCapacity();
        a1=a2=a3=a4=a5=a6=a7=a8=a9=a10=a11=a12=a13=a14=a15=a16=a17=a18=a19=a20=a21=a22=a23=a24=a25=a26=a27=a28=a29=a30=a31=a32=a33=a34=a35=a36=a37=a38=a39=a40=a41=a42=a43=a44=a45=a46=a47=a48=a49=a50=a51=a52=a53=a54=a55=a56=a57=a58=a59=a60=a61=a62=a63=a64=a65=a66=a67=a68=a69=a70=a71=a72=a73=a74=a75=a76=a77=a78=a79=a80=a81=a82=a83=a84=a85=a86=a87=a88=a89=a90=a91=a92=a93=a94=a95=a96=a97=a98=a99=a100=0;
    }

    public void dontStop() {
        while (true) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //不停创建线程,每个线程有内存占用
    public void stackLeakByThread() {
        while (true) {
            System.out.println("创建线程");
            new Thread(this::dontStop).start();
        }
    }

    public static void main(String[] args) {
        JavaVMStackSOF sof = new JavaVMStackSOF();
        try {
            //栈深度测试
//            sof.stackLeak();
            //栈容量测试
//            sof.stackCapacity();
            //通过线程数量致使内存溢出,将栈大小设大一点 -Xss20M
            sof.stackLeakByThread();
        } catch (Exception e) {
            System.out.println("stack length:" + sof.stackLength);
            throw e;
        }
    }
}
